package com.wht.controller;

import com.alibaba.fastjson.JSONObject;
import com.wht.service.DtsGoodsProductService;
import com.wht.service.DtsGoodsService;
import com.wht.service.DtsOrderService;
import com.wht.service.DtsUserService;
import com.wht.util.AuthSupport;
import com.wht.util.ResponseUtil;
import com.wht.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author wanghaotian
 * time:2022/12/02,0002 15:02
 * description:
 */

@RestController
@CrossOrigin("*")
@RequestMapping("/admin/dashboard")
public class AdminDashboardController {
    private static final Logger logger = LoggerFactory.getLogger(AdminDashboardController.class);
    private static final int STATIC_DAYS_RANG = 30;

    @Autowired
    private DtsUserService userService;

    @Autowired
    private DtsGoodsService goodsService;

    @Autowired
    private DtsGoodsProductService goodsProductService;

    @Autowired
    private DtsOrderService orderService;

    @GetMapping("")
    public Object info(){
        logger.info("【请求开始】操作人:[" + AuthSupport.userName() + "] 系统管理->首页仪表盘查");

        int userTotal = userService.count();
        int goodsTotal = goodsService.count();
        int productTotal = goodsProductService.count();
        int orderTotal = orderService.count();
        Map<String,Object> data = new HashMap<>();
        data.put("userTotal",userTotal);
        data.put("goodsTotal",goodsTotal);
        data.put("productTotal",productTotal);
        data.put("orderTotal",orderTotal);

        logger.info("【请求结束】系统管理->首页仪表盘查询:响应结果:{}", JSONObject.toJSONString(data));
        return ResponseUtil.ok(data);
    }

    @GetMapping("/chart")
    public Object chart(){
        logger.info("【请求开始】操作人:[" + AuthSupport.userName() + "] 系统管理->首页图表查询");
        UserOrderCntVo userOrderCnt = new UserOrderCntVo();
        List<DayStatis> userCnts = userService.recentCount(STATIC_DAYS_RANG);
        List<DayStatis> orderCnts = orderService.recentCount(STATIC_DAYS_RANG);
        String[] dayData = unionDayData(userCnts,orderCnts);
        userOrderCnt.setDayData(dayData);
        userOrderCnt.setUserCnt(fetchArrCnt(dayData, userCnts));
        userOrderCnt.setOrderCnt(fetchArrCnt(dayData, orderCnts));
        OrderAmtsVo orderAmts = fetchOrderAmtsVo(orderCnts);
        List<CategorySellAmts> categorySellStatis = orderService.cateGorySell();
        CategorySellVo categorySell = fetchCategorySell(categorySellStatis);
        Map<String,Object> data = new HashMap<>();
        data.put("userOrderCnt",userOrderCnt);
        data.put("orderAmts",orderAmts);
        data.put("categorySell",categorySell);
        logger.info("【请求结束】系统管理->首页图表查询:响应结果:{}",JSONObject.toJSONString(data));
        return ResponseUtil.ok(data);
    }

    private String[] unionDayData(List<DayStatis> userCnts,List<DayStatis> orderCnts){
        Set<String> days = new HashSet<>();
        for (DayStatis userCnt : userCnts) {
            days.add(userCnt.getDayStr());
        }
        for (DayStatis orderCnt : orderCnts) {
            days.add(orderCnt.getDayStr());
        }
        List<String> list = new ArrayList<>(days);
        Collections.sort(list);
        return list.toArray(new String[0]);
    }

    private int[] fetchArrCnt(String[] dayData,List<DayStatis> dayStatisCnts){

        int[] arrCnts = new int[dayData.length];
        for (int i = 0; i < dayData.length; i++) {
            int dayCnt = 0;
            String dayStr = dayData[i];
            for (DayStatis ds : dayStatisCnts) {
                if (dayStr.equals(ds.getDayStr())) {
                    dayCnt = ds.getCnts();
                    break;
                }
            }
            arrCnts[i] = dayCnt;
        }
        return arrCnts;
    }

    private OrderAmtsVo fetchOrderAmtsVo(List<DayStatis> orderCnts){
        OrderAmtsVo orderAmts = new OrderAmtsVo();
        int size = 0;
        if(orderCnts != null && orderCnts.size() > 0){
            size = orderCnts.size();
        }
        String[] dayData = new String[size];
        int [] orderCntData = new int[size];
        BigDecimal[] orderAmtData = new BigDecimal[size];
        for (int i = 0; i < size; i++) {
            dayData[i] = orderCnts.get(i).getDayStr();
            orderCntData[i] = orderCnts.get(i).getCnts();
            orderAmtData[i] = orderCnts.get(i).getAmts();
        }
        orderAmts.setDayData(dayData);
        orderAmts.setOrderCntData(orderCntData);
        orderAmts.setOrderAmtData(orderAmtData);
        return orderAmts;
    }

    private CategorySellVo fetchCategorySell(List<CategorySellAmts> categorySellData){
        CategorySellVo categorySell = new CategorySellVo();
        int size = 0;
        if(categorySellData !=null && categorySellData.size() > 0){
            size = categorySellData.size();
        }
        String[] categoryNames = new String[size];
        for (int i = 0; i < size; i++) {
            categoryNames[i] = categorySellData.get(i).getName();
        }
        categorySell.setCategoryNames(categoryNames);
        categorySell.setCategorySellData(categorySellData);
        return categorySell;
    }

}
